//
//  SDHSportsTeamData.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/9.
//

#import "SDHSportsTeamDataVC.h"
#import "SDHSportsTeamDataCell.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
//#import "SDHSportsTeamDataModel.h"
#import "SDHSportsPlayerModel.h"
#import "MJExtension.h"
#import "SDHSportsPlayerInfoVC.h"

@interface SDHSportsTeamDataVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *dataTable;
//@property (nonatomic, strong) SDHSportsTeamDataModel *dataModel;
@property (nonatomic, strong) NSMutableArray *playersArray;

@end

@implementation SDHSportsTeamDataVC

- (id)init {
    if (self = [super init]) {
        _playersArray = [NSMutableArray array];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    UITableView *table = [UITableView new];
    table.backgroundColor = [[UIColor whiteColor] addAlpha:0.7];
    table.dataSource = self;
    table.delegate = self;
    [self.view addSubview:table];
    self.dataTable = table;
    
    __weak typeof(self) weakSelf = self;
    [self.dataTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view).insets(UIEdgeInsetsZero);
        make.bottom.equalTo(weakSelf.view.mas_top).offset(kScreenHeight-255);
    }];
    
    [self loadData];
}

- (void)loadData {
    SDHSportsRequest *request = [[SDHSportsRequest alloc] init];

    // http://jsuywu8.com/guojitiyu/Tiyusaishi/teamData?id=127136&league_id=200
    request.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyusaishi/teamData";
    NSString *team_id = self.tabInfoDic[@"teamInfo"][@"team_id"];
    NSString *league_id = self.tabInfoDic[@"teamInfo"][@"league_id"];
    if (team_id.length == 0) {
        team_id = @"127136";
        league_id = @"200";
    }
    request.requestPara = @{@"id":team_id, @"league_id":league_id};
    
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showLoading];
    [[SDHSportsNetRequest sharedRequest] sendRequest:request returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
        
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
}

- (void)parseData:(NSDictionary *)dic {
//    _dataModel = [SDHSportsTeamDataModel mj_objectWithKeyValues:dic[@"data"]];
    _playersArray = [SDHSportsPlayerModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
}

- (void)endLoading {
    [MBProgressHUD hideHUD];
    
    [self.dataTable reloadData];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
}


#pragma mark - tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _playersArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsTeamDataCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SDHSportsTeamDataCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
//    SDHSportsTeamDataPlayerModel *model = [_dataModel.players objectAtIndex:indexPath.row];
    SDHSportsPlayerModel *model = [_playersArray objectAtIndex:indexPath.row];
    [cell updateContent:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SDHSportsPlayerModel *model = [_playersArray objectAtIndex:indexPath.row];
    SDHSportsPlayerInfoVC *vc = [[SDHSportsPlayerInfoVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
